﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Sita.MainModule.Services;
using Sita.Entities;
using Sita.Presentation.ViewModels;

namespace Sita.Presentation.Controllers
{
    public class VehicleController : DefaultController
    {
        VehicleService vehicleService;

        public VehicleController()
        {
            vehicleService = new VehicleService();
        }

        public ActionResult Index(int? customerID)
        {
            if (customerID.HasValue)
            {
                Session["Vehicle_CustomerID"] = customerID.Value;
                CustomerService customerService = new CustomerService();

                ViewData["CustomerName"] = "de " + customerService.GetCustomerByID(customerID.Value).FullName;
            }
            else
            {
                Session["Vehicle_CustomerID"] = 0;
                ViewData["CustomerName"] = "";
            }
            return View();
        }

        public PartialViewResult List(int? index, string sortColumn, bool? sortAscending, int? pageSize)
        {

            int TotalRows;

            int CurrentBusinessUnitID = this.AuthenticatedUser.Business[this.AuthenticatedUser.CurrentBusinessUnitIndex].BusinessUnitID;
            int CustomerID = int.Parse(Session["Vehicle_CustomerID"].ToString());

            if (string.IsNullOrEmpty(sortColumn))
                sortColumn = "LicencePlate";

            sortAscending = sortAscending ?? true;

            index = index ?? 0;

            if (!pageSize.HasValue)
            {
                pageSize = 5;
            }

            ViewData["PageSizeFilter"] = new SelectList(new int[] { 5, 10, 15, 20 });

            return PartialView(
                    new GenericGridList<Vehicle>(
                        vehicleService.GetPaginatedVehiclesByBussinesUnitID(CurrentBusinessUnitID, index.Value, pageSize.Value, sortColumn, sortAscending.Value, CustomerID, out TotalRows), index.Value, sortColumn, pageSize.Value, TotalRows, sortAscending.Value));
        }

        #region CREATE
        [HttpGet]
        public PartialViewResult Create()
        {
            CustomerService CustomerService = new CustomerService();
            VehicleTypeService VehicleTypeService = new VehicleTypeService();
            VehicleBrandService VehicleBrandService = new VehicleBrandService();

            ViewData["Customers"] = new SelectList(CustomerService.GetCustomersByBusinessUnitID(this.AuthenticatedUser.Business[this.AuthenticatedUser.CurrentBusinessUnitIndex].BusinessUnitID, string.Empty), "CustomerID", "FullName", null);
            ViewData["VehicleTypes"] = new SelectList(VehicleTypeService.GetAll(), "VehicleTypeID", "Name", null);
            ViewData["VehicleBrands"] = new SelectList(VehicleBrandService.GetAll(), "BrandID", "BrandName", null);

            return PartialView("Create");
        }

        [HttpPost]
        public JsonResult Create(Vehicle vehicle)
        {
            JsonResult Response;

            vehicle.BusinessUnitID = this.AuthenticatedUser.Business[this.AuthenticatedUser.CurrentBusinessUnitIndex].BusinessUnitID;

            vehicleService.AddVehicle(vehicle);

            Response = new JsonResult();
            Response.JsonRequestBehavior = JsonRequestBehavior.AllowGet;

            Response.Data = vehicle.VehicleID > 0;

            return Json(Response);
        }
        #endregion

        #region EDIT
        [HttpGet]
        public PartialViewResult Edit(int? id)
        {
            if (id.HasValue)
            {
                Vehicle selectedVehicle = vehicleService.GetVehicleByID(id.Value);
                CustomerService CustomerService = new CustomerService();
                VehicleTypeService VehicleTypeService = new VehicleTypeService();
                VehicleBrandService VehicleBrandService = new VehicleBrandService();

                ViewData["Customers"] = new SelectList(CustomerService.GetCustomersByBusinessUnitID(this.AuthenticatedUser.Business[this.AuthenticatedUser.CurrentBusinessUnitIndex].BusinessUnitID, string.Empty), "CustomerID", "FullName", null);
                ViewData["VehicleTypes"] = new SelectList(VehicleTypeService.GetAll(), "VehicleTypeID", "Name", null);
                ViewData["VehicleBrands"] = new SelectList(VehicleBrandService.GetAll(), "BrandID", "BrandName", null);

                return PartialView(selectedVehicle);
            }
            else
            {
                //return PartialView("PageNotFound", "Errors");
                return null;
            }
        }

        [HttpPost]
        public JsonResult Edit(Vehicle vehicle)
        {
            Vehicle OriginalVehicle;
            JsonResult Response;

            OriginalVehicle = vehicleService.GetVehicleByID(vehicle.VehicleID);

            Response = new JsonResult();
            Response.JsonRequestBehavior = JsonRequestBehavior.AllowGet;

            if (OriginalVehicle != null)
            {
                TryUpdateModel<Vehicle>(OriginalVehicle);

                vehicleService.ModifyVehicle(OriginalVehicle);

                Response.Data = true;
            }
            else
            {
                Response.Data = false;
            }

            return Json(Response);
        }

        #endregion

        public JsonResult Delete(int? id)
        {
            JsonResult JsonResponse;
            Entities.Vehicle DeleteVehicle;
            Dictionary<string, object> ResultDetails;

            ResultDetails = new Dictionary<string, object>();

            ResultDetails.Add("Message", string.Empty);
            ResultDetails.Add("Success", string.Empty);

            JsonResponse = new JsonResult();

            JsonResponse.JsonRequestBehavior = JsonRequestBehavior.AllowGet;

            if (id.HasValue)
            {
                DeleteVehicle = vehicleService.GetVehicleByIDWithWO(id.Value);

                if (DeleteVehicle != null)
                {
                    if (DeleteVehicle.WorkOrders.Count > 0)
                    {
                        ResultDetails["Success"] = false;
                        ResultDetails["Message"] = Resources.Vehicle.Index.ConfirmDeleteWithChildsMessage;
                    }
                    else
                    {
                        if (vehicleService.DeleteVehicle(DeleteVehicle))
                        {
                            ResultDetails["Success"] = true;
                        }
                        else
                        {
                            ResultDetails["Success"] = false;
                            ResultDetails["Message"] = Resources.Vehicle.Index.CannotDelete;
                        }
                    }
                }
                else
                {
                    ResultDetails["Success"] = false;
                    ResultDetails["Message"] = Resources.Vehicle.Index.InvalidVehicle;
                }
            }
            else
            {
                ResultDetails["Success"] = false;
            }

            JsonResponse.Data = ResultDetails;

            return JsonResponse;
        }
    }
}
